/*
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 *
 * Copyright 2009 - 2012 Luca Mingardi.
 *
 * This file is part of jeeObserver.
 *
 * JeeObserver is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * JeeObserver is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 *
 */
package jeeobserver.server.settings;

import java.io.Serializable;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

/**
 * The Class TimePeriod.
 *
 * @author Luca Mingardi
 * @version 4.0
 */
public final class TimePeriod implements Serializable {

	/** The Constant serialVersionUID. */
	private static final long serialVersionUID = 40L;

	/** The Constant UNIT_MILLISECOND. */
	public static final int UNIT_MILLISECOND = Calendar.MILLISECOND;

	/** The Constant UNIT_SECOND. */
	public static final int UNIT_SECOND = Calendar.SECOND;

	/** The Constant UNIT_MINUTE. */
	public static final int UNIT_MINUTE = Calendar.MINUTE;

	/** The Constant UNIT_HOUR. */
	public static final int UNIT_HOUR = Calendar.HOUR;

	/** The Constant UNIT_DAY. */
	public static final int UNIT_DAY = Calendar.DAY_OF_YEAR;

	/** The Constant UNIT_MONTH. */
	public static final int UNIT_MONTH = Calendar.MONTH;

	/** The Constant UNIT_YEAR. */
	public static final int UNIT_YEAR = Calendar.YEAR;

	/** The value. */
	private int value;

	/** The unit. */
	private int unit;

	/**
	 * Instantiates a new time period.
	 *
	 * @param unit the unit
	 * @param value the value
	 */
	public TimePeriod(int unit, int value) {
		this.value = value;
		this.unit = unit;
	}

	/**
	 * Adds the to date.
	 *
	 * @param date the date
	 * @return the date
	 */
	public Date addToDate(Date date) {

		final Calendar calendar = new GregorianCalendar();

		calendar.setTime(date);
		if (this.unit == TimePeriod.UNIT_MILLISECOND) {
			calendar.add(Calendar.MILLISECOND, this.value);
		} else if (this.unit == TimePeriod.UNIT_SECOND) {
			calendar.add(Calendar.SECOND, this.value);
		} else if (this.unit == TimePeriod.UNIT_MINUTE) {
			calendar.add(Calendar.MINUTE, this.value);
		} else if (this.unit == TimePeriod.UNIT_HOUR) {
			calendar.add(Calendar.HOUR_OF_DAY, this.value);
		} else if (this.unit == TimePeriod.UNIT_DAY) {
			calendar.add(Calendar.DAY_OF_YEAR, this.value);
		} else if (this.unit == TimePeriod.UNIT_MONTH) {
			calendar.add(Calendar.MONTH, this.value);
		} else if (this.unit == TimePeriod.UNIT_YEAR) {
			calendar.add(Calendar.YEAR, this.value);
		} else {
			return null;
		}

		return calendar.getTime();
	}

	/**
	 * Subtract to date.
	 *
	 * @param date the date
	 * @return the date
	 */
	public Date subtractToDate(Date date) {

		final Calendar calendar = new GregorianCalendar();

		calendar.setTime(date);
		if (this.unit == TimePeriod.UNIT_MILLISECOND) {
			calendar.add(Calendar.MILLISECOND, -this.value);
		} else if (this.unit == TimePeriod.UNIT_SECOND) {
			calendar.add(Calendar.SECOND, -this.value);
		} else if (this.unit == TimePeriod.UNIT_MINUTE) {
			calendar.add(Calendar.MINUTE, -this.value);
		} else if (this.unit == TimePeriod.UNIT_HOUR) {
			calendar.add(Calendar.HOUR_OF_DAY, -this.value);
		} else if (this.unit == TimePeriod.UNIT_DAY) {
			calendar.add(Calendar.DAY_OF_YEAR, -this.value);
		} else if (this.unit == TimePeriod.UNIT_MONTH) {
			calendar.add(Calendar.MONTH, -this.value);
		} else if (this.unit == TimePeriod.UNIT_YEAR) {
			calendar.add(Calendar.YEAR, -this.value);
		} else {
			return null;
		}

		return calendar.getTime();
	}

	/**
	 * Gets the millis.
	 *
	 * @return the millis
	 */
	public long getMillis() {

		if (this.unit == TimePeriod.UNIT_MILLISECOND) {
			return this.value;
		} else if (this.unit == TimePeriod.UNIT_SECOND) {
			return this.value * 1000;
		} else if (this.unit == TimePeriod.UNIT_MINUTE) {
			return this.value * 1000 * 60;
		} else if (this.unit == TimePeriod.UNIT_HOUR) {
			return this.value * 1000 * 60 * 60;
		} else if (this.unit == TimePeriod.UNIT_DAY) {
			return this.value * 1000 * 60 * 60 * 24;
		} else if (this.unit == TimePeriod.UNIT_MONTH) {
			return this.value * 1000 * 60 * 60 * 24 * 30;
		} else if (this.unit == TimePeriod.UNIT_YEAR) {
			return this.value * 1000 * 60 * 60 * 24 * 365;
		}
		return 0;
	}

	/**
	 * Gets the nanos.
	 *
	 * @return the nanos
	 */
	public long getNanos() {

		final long millis = this.getMillis();

		return millis * 1000000;
	}

	/**
	 * Gets the unit.
	 *
	 * @return the unit
	 */
	public int getUnit() {
		return this.unit;
	}

	/**
	 * Gets the value.
	 *
	 * @return the value
	 */
	public int getValue() {
		return this.value;
	}

	/**
	 * Sets the unit.
	 *
	 * @param unit the new unit
	 */
	public void setUnit(int unit) {
		this.unit = unit;
	}

	/**
	 * Sets the value.
	 *
	 * @param value the new value
	 */
	public void setValue(int value) {
		this.value = value;
	}
}
